package com.konne.nightmare.FastPublicOpinion.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.konne.nightmare.FastPublicOpinion.utils.HideUtil;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;
import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.ButterKnife;

public abstract class BaseMvpFragment<V extends IBaseMvpView, P extends BaseMvpPresenter<V>> extends RxFragment implements IBaseMvpView, IFragment {

    protected P mPresenter;

    protected View rootView;// 缓存Fragment view
    protected LoadProgressDialog dialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayout(), null);
        }
        // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        if (rootView instanceof ViewGroup) {
            HideUtil.init(getActivity(), (ViewGroup) rootView);//初始化触摸关闭软键盘
        }

        if (mPresenter == null) {
            mPresenter = createPresenter();
        }
        mPresenter.attachMvpView((V) this);

        dialog = new LoadProgressDialog(getActivity(), "请稍后...", false);

        ButterKnife.bind(this, rootView);

//        initImmersionBar();
        initView();
        initData(savedInstanceState);
        return rootView;
    }

    public   <T extends View> T findViewById(@IdRes int id){
        if(rootView==null){
            return null;
        }else {
            return rootView.findViewById(id);
        }
    }
    protected abstract P createPresenter();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachMvpView();
        }
    }

    /**
     * 初始化沉浸式
     * Init immersion bar.
     */
    protected void initImmersionBar() {
//        ImmersionBar.with(this).init();
    }


    public void forward(Fragment fragment, boolean hide) {
        forward(getId(), fragment, null, hide);
    }

    public void forward(int viewId, Fragment fragment, String name, boolean hide) {
        FragmentTransaction trans = getFragmentManager().beginTransaction();
        if (hide) {
            trans.hide(this);
            trans.add(viewId, fragment);
        } else {
            trans.replace(viewId, fragment);
        }

        trans.addToBackStack(name);
        trans.commitAllowingStateLoss();
    }

    public void backward() {
        getFragmentManager().popBackStack();
    }

}
